package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    DeptService deptService;

    @Autowired
    EmpExprMapper empExprMapper;

    @Override
    public PageResult pageResult(EmpQueryParam param) {
        //获取分页对象
        IPage<Emp> pageInfo = new Page<>(param.getPage(), param.getPageSize());

        //设置条件查询条件
        LambdaQueryWrapper<Emp> wrapper = Wrappers.lambdaQuery();
        wrapper.like(param.getName() != null,Emp::getName, param.getName())
                .eq( param.getGender() != null,Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null,Emp::getEntryDate,param.getBegin(), param.getEnd())
                .orderByDesc(Emp::getUpdateTime);
        IPage<Emp> empIPage = this.page(pageInfo,wrapper);

        //获取总数据数，和每页数据
        long total = empIPage.getTotal();
        List<Emp> empList = empIPage.getRecords();

        //判断是否有数据，有则查询对应的部门名称
        if (!CollectionUtils.isEmpty(empList)) {
            //根据对应部门id，获取部门名称
            List<Integer> deptIds = empList.stream().map(emp -> emp.getDeptId()).toList();
            deptIds = deptIds.stream().distinct().toList();
            List<Dept> deptList = deptService.listByIds(deptIds);
            //进行双循环来获取对应的部门名称
            empList.forEach(emp -> {
                deptList.stream().filter(dept -> dept.getId().equals(emp.getDeptId()))
                        .findFirst()
                        .ifPresent(dept -> emp.setDeptName(dept.getName()));
            });

        }

        return new PageResult<>(total, empList);
    }

    @Override
    @Transactional
    public void saveEmp(Emp emp) {
        //补全数据
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        emp.setPassword("123456");
        //保存员工数据
        this.save(emp);

        List<EmpExpr> exprList = emp.getExprList();
        //判断是否有工作经历
        if (!CollectionUtils.isEmpty(exprList)) {
            //为工作经历的员工id赋值
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            //批量新增工作经历
            empExprMapper.insert(exprList);
        }
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        //删除员工信息
        this.removeByIds(ids);
        //删除工作经历
        empExprMapper.delete(Wrappers.<EmpExpr>lambdaQuery().in(EmpExpr::getEmpId, ids));
    }

    @Override
    @Transactional
    public Emp findById(Integer id) {
        //查询员工信息
        Emp emp = this.getById(id);
        //查询工作经历信息
        List<EmpExpr> exprList = empExprMapper.selectList(Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, id));
        emp.setExprList(exprList);
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        //更新员工信息
        this.updateById(emp);
        //删除员工工作经历
        empExprMapper.delete(Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, emp.getId()));
        //判断是否有工作经历，来新增工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insert(exprList);
        }

    }
}
